/******************************************************************************
* Copyright (C) 2020, BBI Technology Co.,Ltd All rights reserved.
*
* Author: caizhiwei.
*/
/******************************************************************************/
/** \file main.c
 

 Note： 程序正式发布的时候,需要去掉工程Option中的 DEBUG 宏定义
 
 
 1. 第一次上电,需要开启NB校时
 
 2. 
 
 ******************************************************************************/
 
#include "main.h"


//#define DEBUG


void vTask_Router(void *pvParameters);
void vTask_valve(void *pvParameters);

void APP_WdtInit(void);
void APP_Check_ResetSource(void);


//时钟配置初始化: 内部时钟PLL到48MHz
void SysClock_Config_48Mhz(void)
{
    en_flash_waitcycle_t      enWaitCycle;
    stc_sysctrl_pll_cfg_t     stcPLLCfg;
    
    //结构体初始化清零
    DDL_ZERO_STRUCT(stcPLLCfg);
    
    enWaitCycle = FlashWaitCycle1;
    Flash_WaitCycle(enWaitCycle);
    
    stcPLLCfg.enInFreq    = SysctrlPllInFreq4_6MHz;     //RCH 4MHz
    stcPLLCfg.enOutFreq   = SysctrlPllOutFreq36_48MHz;  //PLL 输出48MHz
    stcPLLCfg.enPllClkSrc = SysctrlPllRch;              //输入时钟源选择RCH
    stcPLLCfg.enPllMul    = SysctrlPllMul12;            //4MHz x 12 = 48MHz
    Sysctrl_SetPLLFreq(&stcPLLCfg);
    Sysctrl_SetPLLStableTime(SysctrlPllStableCycle16384);
    Sysctrl_ClkSourceEnable(SysctrlClkPLL, TRUE);
    
    Sysctrl_SysClkSwitch(SysctrlClkPLL);  ///< 时钟切换
}

void App_GpioCfg(void)
{
    stc_gpio_cfg_t         GpioInitStruct;
    DDL_ZERO_STRUCT(GpioInitStruct);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);  //GPIO外设时钟打开
    
    //PD5 配置LED灯端口
    GpioInitStruct.enDrv  = GpioDrvH;
    GpioInitStruct.enDir  = GpioDirOut;
    Gpio_Init(STK_LED_PORT, STK_LED_PIN, &GpioInitStruct);
    Gpio_WriteOutputIO(STK_LED_PORT, STK_LED_PIN, FALSE);   //配置LED灯默认熄灭状态
	
}
/*mbus主站是非持续供电的,那么MCU上电后必须快速初始化uart*/


int main(void)
{	

	SysClock_Config_48Mhz();

	delay1ms(500); 

#ifdef DEBUG
    SEGGER_RTT_Init();	
#endif
	
	
	
//	APP_WdtInit();
	
	//App_GpioCfg();       //配置GPIO，按键按下，才可以进行运行
 
	//BSP_KEY_GPIO_init();
	
	//ATClient_Init();
	
	BSP_LCD_init();
	
	BSP_UARTx_Init(M0P_UART1,38400, UartMskNone); /*用于调试打印*/

	printf("\r\nHello,HC32L136!\r\n");
	
	Lcd_ClearDisp();             ///< 清屏
    Lcd_WriteRam(0,0x0f0f0f0f);  ///< 赋值寄存器LCDRAM0
    Lcd_WriteRam(1,0x0f0f0f0f);  ///< 赋值寄存器LCDRAM1


	//BSP_IntelligentRtc_init();
	
	//AT_SendCmd(&iccid_cfg);
	//printf("\r\n len1 = %d",sizeof(uint16_t));

	mcu_get_version_date();
	
	//Lpm_GotoDeepSleep(FALSE);       //进入低功耗模式

	//usart_Queue = xQueueCreate(QueueSIZE, sizeof(uint8_t));
	
	xTaskCreate(vTask_Router, "Router", 200, NULL, 1, NULL); 		/* 创建任务1,优先级为2 */
	xTaskCreate(vTask_valve, "Valve",128/*栈空间(Word)*/, NULL, 2, NULL);  /* 创建任务2,优先级为1 */
	
	vTaskStartScheduler(); /* 启动任务调度 */
	
	while(1);
	
	
	
}


#ifdef RTOS

void vTask_Router(void *pvParameters)
{	
	static int old_min = 0;
	struct tm stctim;
	
	stctim.tm_year = 121;
	stctim.tm_mon = 1;
	stctim.tm_mday = 28;
	
	stctim.tm_hour = 23;
	stctim.tm_min = 59;
	stctim.tm_sec = 50;

//	#ifdef DEBUG_MODE                                                        //调试模式
    SEGGER_RTT_printf(0,"Demo Init!\r\n");
//    #endif   
	
	while (1)
    {

		

//        SEGGER_RTT_SetTerminal(0);     
        SEGGER_RTT_printf(0,"Demo Run Terminal 0!\r\n");   
//        SEGGER_RTT_SetTerminal(1);     
//        SEGGER_RTT_printf(0,"Demo Run Terminal 1!\r\n");         


//		APP_LocalCOM_ReadData();
//		Wdt_Feed(); 
	    // portMAX_DELAY参数:使任务一直处于就绪态,能及时接收信号量	
        //if(xSemaphoreTake(BinSem_UART_Idle,pdMS_TO_TICKS(10)))		
		//if(xSemaphoreTake(BinSem_UART_Idle,portMAX_DELAY)) 
		//{
		//	 printf("Received Data!\r\n");
		//	 BSP_Rtc_SetTime(&stctim);
		//}
		
		printf("Task1 is Running!\r\n");	
		vTaskDelay(400);   		 /*延时400ms，延时期间任务进入阻塞态 */		
    }
 }

// 此任务用于接收信号量,并执行开阀或关阀操作
void vTask_valve(void *pvParameters)
{
   //APP_Valve_SelfCheck();	
	
	#ifdef DEBUG
		
	char pcWriteBuffer[128];  /*打印缓存*/
		
	#endif
	
   while(1)
   {       	
//	   APP_Valve_control();  Lpm_GotoDeepSleep(FALSE);  
//	   APP_Valve_Check();
	   vTaskDelay(1000);	
	   
#ifdef DEBUG
	   
		vTaskList((char *)&pcWriteBuffer);
		printf("Task  	  Status   	Priority  Stack(Word)  Task_Seq\r\n");
		printf("%s\r\n", pcWriteBuffer);   
#endif
	   
	    //APP_TimeSync();

	   //printf("Date = %d-%d-%d\r\n",stcDate.tm_year + 1900,stcDate.tm_mon,stcDate.tm_mday);	   	  	   
	   //printf("Time = %d:%d:%d\r\n",stcDate.tm_hour,stcDate.tm_min,stcDate.tm_sec);	 

       //tblock = localtime(&stcDate);
	   
//　　printf("Local time is: %s\n",asctime(tblock));
   
   }
} 

#endif




